﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZWZ.Web.WebAdmin.UCtrs
{
    public partial class MsgList : System.Web.UI.UserControl
    {
        private const string DEFAULTIMGURL = "/webadmin/images/nopic.jpg";
        public Unit Height { get; set; }
        public string MsgUserID
        {
            get
            {
                return ViewState["MsgUserID"].ToString();
            }
            set
            {
                ViewState["MsgUserID"] = value;
            }
        }
        public ModelBase.User_Info MsgUserInfo
        {
            get
            {
                if (ViewState["MsgUserInfo"] == null)
                {
                    ViewState["MsgUserInfo"] = DBProvider.DB.Ctx.User_Info.Where(i => i.ID == MsgUserID).FirstOrDefault();
                }
                return ViewState["MsgUserInfo"] as ModelBase.User_Info;
            }
            set
            {
                ViewState["MsgUserInfo"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LabTitle.Text = string.Format("与 {0} 聊天中…", MsgUserInfo.NickName);
                DataBind();
            }
        }
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Image ImgPic = e.Item.FindControl("ImgPic") as Image;
                Panel PanItem = e.Item.FindControl("PanItem") as Panel;
                var msg = (ModelBase.User_Message)e.Item.DataItem;
                if (msg.SenderID == Codes.LoginHelper.CurrentUser.ID)
                {
                    PanItem.CssClass = "rightitem";
                    ImgPic.AlternateText = Codes.LoginHelper.CurrentUser.NickName;
                    ImgPic.ImageUrl = string.IsNullOrEmpty(Codes.LoginHelper.CurrentUser.PicUrl) ? DEFAULTIMGURL : Codes.LoginHelper.CurrentUser.PicUrl;
                }
                else
                {
                    PanItem.CssClass = "leftitem";
                    ImgPic.AlternateText = MsgUserInfo.NickName;
                    ImgPic.ImageUrl = string.IsNullOrEmpty(MsgUserInfo.PicUrl) ? DEFAULTIMGURL : MsgUserInfo.PicUrl;
                }
            }
        }
        public override void DataBind()
        {
            string CUserID = Codes.LoginHelper.CurrentUser.ID;
            var data=DBProvider.DB.Ctx.User_Message
                .Where(i =>
                    (i.ReciverID == MsgUserID && i.SenderID == CUserID) ||
                    (i.ReciverID == CUserID && i.SenderID == MsgUserID)
                    ).OrderBy(i=>i.SentTime).ToList();
            data.ForEach(arg => { if(arg.ReciverID==CUserID) arg.Status = ZWZ.Enums.User.MessageStatus.已发送已读.ToString(); });
            DBProvider.DB.Ctx.SaveChanges();
            Repeater1.DataSource = data;
            Repeater1.DataBind();
            base.DataBind();
        }
        protected void BtnSend_Click(object sender, EventArgs e)
        {
            string MessageBody = Request.Form["myContent"];
            var umsg = new ModelBase.User_Message()
            {
                ID = Codes.IDHelper.CreateID_UserMessage(),
                SenderID = Codes.LoginHelper.CurrentUser.ID,
                SenderName = Codes.LoginHelper.CurrentUser.NickName,
                ReciverID = MsgUserInfo.ID,
                ReciverName = MsgUserInfo.NickName,
                Title = hidTitle.Value,
                Body = MessageBody,
                CreateTime = DateTime.Now,
                SentTime = DateTime.Now,
                Status = ZWZ.Enums.User.MessageStatus.已发送未读.ToString(),
                Type = ZWZ.Enums.User.MessageType.私信.ToString(),
            };
            DBProvider.DB.Ctx.User_Message.AddObject(umsg);
            DBProvider.DB.Ctx.SaveChanges();
            DataBind();
        }
    }
}